package com.ts.api.http.feedback;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.log.LogLevel;
import com.gitee.apanlh.util.net.http.HttpClientBuilder;
import com.gitee.apanlh.util.net.http.HttpResponse;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.http.HttpMethod;
import com.gitee.apanlh.web.model.vo.RequestVO;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 知乎
 *
 * https://xg.zhihu.com/openApiDoc/apiServiceDoc
 *
 * @author caoxin
 */
@Component
public class ZhiHuFeedbackHandler implements FeedbackHandler {

    @Override
    public boolean match(RequestVO requestVO) {
        return Eq.str(requestVO.getStr("type"), "zhihu");
    }

    @Override
    public Object feedback(RequestVO requestVO) {
        String cb = requestVO.getStr("cb");
        if (ValidParam.isNotEmpty(cb)) {
            String eventType = "api_submit";
            String timestamp = String.valueOf((int) (System.currentTimeMillis() / 1000));
            String url = cb.replace("__EVENTTYPE__", eventType).replace("__TIMESTAMP__", timestamp);
            HttpResponse build = HttpClientBuilder.builder(url, HttpMethod.GET)
                    .withLogLevel(LogLevel.WARN)
                    .withErrorLogLevel(LogLevel.WARN)
                    .build();
            String str = build.getStr();
            JSONObject jsonObject = JSONObject.parseObject(str);
        }
        return new HashMap<>();
    }

}
